package solutions.leetcode.normal;

import solutions.lib.BaseSolution;

import java.util.LinkedList;
import java.util.List;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/queens-that-can-attack-the-king/">1222. 可以攻击国王的皇后</a>
 */
public class Solution1222 extends BaseSolution {

    public List<List<Integer>> queensAttacktheKing(int[][] queens, int[] king) {
        List<List<Integer>> rst = new LinkedList<>();
        int[][] offsets = {
                {-1, -1},
                {-1, 0},
                {-1, 1},
                {0, 1},
                {1, 1},
                {1, 0},
                {1, -1},
                {0, -1},
        };
        boolean[][] pan = new boolean[8][8];
        for (int[] v : queens) {
            pan[v[0]][v[1]] = true;
        }
        for (int[] offset : offsets) {
            int step = 1;
            while (true) {
                int px = king[0] + offset[0] * step;
                int py = king[1] + offset[1] * step;
                if (px < 0 || px >= 8 || py < 0 || py >= 8) {
                    break;
                }
                step++;
                if (pan[px][py]) {
                    rst.add(getList(px, py));
                    break;
                }
            }
        }
        return rst;
    }

    private List<Integer> getList(int... vals) {
        List<Integer> list = new LinkedList<>();
        for (int v : vals) {
            list.add(v);
        }
        return list;
    }

    public static void main(String[] args) {
        Solution1222 solution = new Solution1222();
    }
}